import vuePlugin from "rollup-plugin-vue";
import json from "@rollup/plugin-json";
import url from "@rollup/plugin-url";
import esbuild from "rollup-plugin-esbuild";
import postcss from "rollup-plugin-postcss";
import replace from "@rollup/plugin-replace";
import { terser } from "rollup-plugin-terser";
import commonjs from "@rollup/plugin-commonjs";
import babel from "@rollup/plugin-babel";

import { DEFAULT_EXTENSIONS } from "@babel/core";
import pkg from "./package.json";
const externalDeps = Object.keys(pkg.dependencies || {});
const externalPeerDeps = Object.keys(pkg.peerDependencies || {});

const plugins = [
  vuePlugin(),
  commonjs(),
  esbuild({
    target: "esnext",
    minify: false,
    jsx: "transform",
    tsconfig: "tsconfig.build.json",
  }),
  babel({
    plugins: ["@babel/plugin-transform-runtime"],
    babelHelpers: "runtime",
    extensions: [...DEFAULT_EXTENSIONS, ".vue", ".ts", ".tsx"],
  }),
  postcss(),
  json(),
  url(),
  replace({
    "process.env.NODE_ENV": JSON.stringify("production"),
    preventAssignment: true,
  }),
  terser(),
];

const external = externalDeps.concat(externalPeerDeps);
const input = ["packages/index.ts"];

const cjsConfig = {
  input,
  external,
  plugins,
  output: {
    dir: "dist",
    format: "cjs",
    name: "@gonsin/glicense",
    sourcemap: true,
    exports: "named",
  },
};

export default [cjsConfig];
